package com.meeting.meet.service.base;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ReflectUtil;

import com.meeting.meet.manager.bo.*;
import com.meeting.meet.manager.bo.AttendeeBO;
import com.meeting.meet.persist.dos.Attendee;
import com.meeting.meet.service.base.BaseAttendeeBOService.CreateAttendeeJoinAgendaBoResult;
import com.meeting.meet.service.bto.CreateAttendeeJoinAgendaBto;
import com.vs.bo.AddedBto;
import com.vs.bo.BaseBoResult;
import com.vs.bo.UnmodifiedBto;
import com.vs.bo.UpdatedBto;
import com.vs.code.AutoGenerated;
import com.vs.code.DoNotModify;
import com.vs.ox.common.exception.IgnoredException;
import com.vs.sqlmapper.core.PgIdGenerator;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Component;

import java.util.Set;

import javax.annotation.Resource;

@DoNotModify
@Slf4j
@Component
@AutoGenerated(locked = true, uuid = "f6675446-0058-38de-a51d-0416819da819")
public class BaseAttendeeBOService {
    @AutoGenerated(locked = true)
    @Resource(name = "pgIdGenerator")
    private PgIdGenerator idGenerator;

    /** 参会者议程 */
    @AutoGenerated(locked = true)
    protected CreateAttendeeJoinAgendaBoResult createAttendeeJoinAgendaBase(
            CreateAttendeeJoinAgendaBto createAttendeeJoinAgendaBto) {
        if (createAttendeeJoinAgendaBto == null) {
            throw new IgnoredException(400, "bto 不能为null!");
        }
        CreateAttendeeJoinAgendaBoResult boResult = new CreateAttendeeJoinAgendaBoResult();
        AttendeeBO attendeeBO =
                createCreateAttendeeJoinAgenda(boResult, createAttendeeJoinAgendaBto);
        boResult.setRootBo(attendeeBO);
        return boResult;
    }

    /** 数据库创建一行, 数据库创建一行 */
    @AutoGenerated(locked = true)
    private AttendeeBO createCreateAttendeeJoinAgenda(
            BaseAttendeeBOService.CreateAttendeeJoinAgendaBoResult boResult,
            CreateAttendeeJoinAgendaBto createAttendeeJoinAgendaBto) {
        AttendeeBO attendeeBO = null;
        boolean found = false;
        boolean allNull = false;
        boolean pkMatched = false;
        boolean pkExist = false;
        String matchedUkName = "";
        if (attendeeBO != null) {
            if (pkMatched) {
                log.error("主键冲突, id:{}的记录在数据库表:{}中已经存在!", attendeeBO.getId(), "meet_attendee");
                throw new IgnoredException(400, "参会者已存在");
            } else {
                log.error(
                        "唯一键UK{}和数据库表:'{}'中id为:{}的记录冲突",
                        matchedUkName,
                        "meet_attendee",
                        attendeeBO.getId());
                throw new IgnoredException(400, "参会者已存在");
            }
        } else {
            attendeeBO = new AttendeeBO();
            if (pkExist) {
                attendeeBO.setId(this.idGenerator.allocateId("meet_attendee"));
            } else {
                attendeeBO.setId(this.idGenerator.allocateId("meet_attendee"));
            }
            if (CollectionUtil.contains(
                    (Set<String>)
                            ReflectUtil.getFieldValue(
                                    createAttendeeJoinAgendaBto, "__$validPropertySet"),
                    "meetId")) {
                attendeeBO.setMeetId(createAttendeeJoinAgendaBto.getMeetId());
            }
            if (CollectionUtil.contains(
                    (Set<String>)
                            ReflectUtil.getFieldValue(
                                    createAttendeeJoinAgendaBto, "__$validPropertySet"),
                    "userId")) {
                attendeeBO.setUserId(createAttendeeJoinAgendaBto.getUserId());
            }
            if (CollectionUtil.contains(
                    (Set<String>)
                            ReflectUtil.getFieldValue(
                                    createAttendeeJoinAgendaBto, "__$validPropertySet"),
                    "agendaId")) {
                attendeeBO.setAgendaId(createAttendeeJoinAgendaBto.getAgendaId());
            }
            AddedBto addedBto = new AddedBto();
            addedBto.setBto(createAttendeeJoinAgendaBto);
            addedBto.setBo(attendeeBO);
            boResult.getAddBtoList().add(addedBto);
        }
        return attendeeBO;
    }

    public static class CreateAttendeeJoinAgendaBoResult extends BaseBoResult {

        @AutoGenerated(locked = true)
        public AttendeeBO getRootBo() {
            return (AttendeeBO) this.rootBo;
        }

        @AutoGenerated(locked = true)
        public AddedBto<CreateAttendeeJoinAgendaBto, AttendeeBO> getCreatedBto(
                CreateAttendeeJoinAgendaBto createAttendeeJoinAgendaBto) {
            return this.getAddedResult(createAttendeeJoinAgendaBto);
        }

        @AutoGenerated(locked = true)
        public Attendee getDeleted_Attendee() {
            return (Attendee) CollectionUtil.getFirst(this.getDeletedEntityList(Attendee.class));
        }

        @AutoGenerated(locked = true)
        public UpdatedBto<CreateAttendeeJoinAgendaBto, Attendee, AttendeeBO> getUpdatedBto(
                CreateAttendeeJoinAgendaBto createAttendeeJoinAgendaBto) {
            return super.getUpdatedResult(createAttendeeJoinAgendaBto);
        }

        @AutoGenerated(locked = true)
        public UnmodifiedBto<CreateAttendeeJoinAgendaBto, AttendeeBO> getUnmodifiedBto(
                CreateAttendeeJoinAgendaBto createAttendeeJoinAgendaBto) {
            return super.getUnmodifiedResult(createAttendeeJoinAgendaBto);
        }
    }
}
